package cn.com.bluemoon.daps.system.controller;

import cn.com.bluemoon.daps.common.constant.DapConstant;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.datascope.IgnorePermission;
import cn.com.bluemoon.daps.common.datascope.log.OperateLog;
import cn.com.bluemoon.daps.common.datascope.log.enums.ModuleObject;
import cn.com.bluemoon.daps.common.datascope.log.enums.OperateType;
import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import cn.com.bluemoon.daps.system.dto.DapSystemPropertyBo;
import cn.com.bluemoon.daps.system.dto.PageRequestDto;
import cn.com.bluemoon.daps.system.entity.DapSystemProperty;
import cn.com.bluemoon.daps.system.service.DapSystemPropertyService;
import cn.com.bluemoon.datasecurity.app.sdk.RemoteCryptoModuleService;
import cn.com.bluemoon.metadata.common.ResultBean;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dapSystem")
public class DapSystemPropertyController{

    @Resource
    DapSystemPropertyService dapSystemPropertyService;

    @Autowired
    RemoteCryptoModuleService remoteCryptoModuleService;

    @ApiOperation("系统参数列表")
    @PostMapping(value = "/property/page")
    @BmPermission(value = "data:security:property:list")
    public ResultBean<IPage<DapSystemProperty>> systemPropertyPage(@RequestBody PageRequestDto param) {

        IPage<DapSystemProperty> dapSystemPropertyIPage = dapSystemPropertyService.systemPropertyPage(param.getPageSize(), param.getPageNum(), param.getKey());
        return new ResultBean(dapSystemPropertyIPage);
    }

    @ApiOperation("新增或更新系统参数")
    @PostMapping(value = "/property/change")
    @BmPermission(value = "data:security:property:change")
    @OperateLog(type = OperateType.UPDATE, moduleName = ModuleObject.SYSTEM)
    public ResultBean<Boolean> insertOrUpdateProperty(@RequestBody DapSystemProperty request) {

        DapSystemProperty dapSystemProperty = new DapSystemProperty();
        BeanUtils.copyProperties(request, dapSystemProperty);
        if(request.getId() == null){
            dapSystemProperty.setCreateUserId(UserInfoHolder.getAccount());
        }else {
            dapSystemProperty.setLastUpdateUserId(UserInfoHolder.getAccount());
        }
        Boolean flag = dapSystemPropertyService.insertOrUpdateProperty(dapSystemProperty);
        if(flag && request.getPropertyGroup().equals(DapConstant.CRYPTO_GROUP)){
            remoteCryptoModuleService.refreshCrypto();
        }
        return flag ? new ResultBean(flag) : new ResultBean(500, String.format("当前参数标识[%s]参数名[%s]重复",
                request.getPropertyGroup(), request.getPropertyKey()));
    }

    @ApiOperation("系统参数详情")
    @GetMapping(value = "/property/info")
    @BmPermission(value = "data:security:property:info")
    public ResultBean<DapSystemProperty> propertyInfo(@RequestParam("id") String id) {

        DapSystemProperty dapSystemProperty = dapSystemPropertyService.propertyInfo(id);
        return new ResultBean(dapSystemProperty);
    }

    @ApiOperation("删除系统参数")
    @DeleteMapping(value = "/property/delete")
    @BmPermission(value = "data:security:property:delete")
    @OperateLog(type = OperateType.DELETE, moduleName = ModuleObject.SYSTEM)
    public ResultBean<Boolean> deleteProperty(@RequestParam("id") String id) {
        return new ResultBean(dapSystemPropertyService.deleteProperty(id));
    }

    @ApiOperation("获取系统字典属性")
    @PostMapping("/getDapSystemProperty")
    public ResultBean<Map<String, List<DapSystemPropertyBo>>> getDapSystemProperty(@RequestBody List<String> propertyGroups) {
        Map<String, List<DapSystemProperty>> dapMap = dapSystemPropertyService.getByPropertyGroups(propertyGroups);
        Map<String, List<DapSystemPropertyBo>> boMap = new HashMap<>();
        if (dapMap != null) {
            dapMap.forEach((k, v) -> boMap.put(k, cn.com.bluemoon.daps.system.utils.BeanUtils.copyListProperties(v, DapSystemPropertyBo::new)));
        }
        return new ResultBean<>(boMap);
    }

    @IgnorePermission(name = "获取脱敏规则列表")
    @ApiOperation("获取脱敏规则列表")
    @GetMapping("/deSensiTypeList")
    public ResultBean getDeSensiTypeList() {

        List<DapSystemProperty> dapSystemProperties = dapSystemPropertyService.getBaseMapper().selectList(new LambdaQueryWrapper<DapSystemProperty>()
                .eq(DapSystemProperty::getPropertyGroup, DapConstant.RULE_TYPE).orderByAsc(DapSystemProperty::getSort));
        return new ResultBean(dapSystemProperties);
    }


}
